#!/bin/bash

myFile=$(find /usr/mcc/update/ -name commandcenter.mcc-adms-web-service.* |xargs ls -alt| head -n 1 | awk '{print $9}' )
echo "there are:$myFile"
OPTSTATUS=200
PGIP=0.0.0.0
PGPORT=5432
PGUSER=postgres
PGPWD=123456
while getopts ":i:v:p:u:w:s:" opt
do
    case $opt in
        i)
        echo "ip--$OPTARG"
  PGIP=$OPTARG;
        ;;
      v)
        echo "version--$OPTARG"
    SERVERVERSION=V$OPTARG;
        ;;
        p)
        echo "port--$OPTARG"
  PGPORT=$OPTARG;
        ;;
        u)
        echo "user--$OPTARG"
  PGUSER=$OPTARG;
        ;;
        w)
        echo "pwd--$OPTARG"
  PGPWD=$OPTARG;
        ;;
        s)
        echo "OPT--$OPTARG"
  OPTSTATUS=$OPTARG;

    read -r -p "Are You Sure? [Y/n] " input
        case $input in
        [yY][eE][sS]|[yY])
            echo $input
            ;;
        [nN][oO]|[nN])
            exit 1
        ;;
        *)
        echo "Invalid input..."
        exit 1
        ;;
        esac
        ;;
        ?)
        echo "---$opt"
        exit 1;;
    esac
done


function stop(){
  /usr/mcc/server/mcc-adms-web-service/bin/shutdown.sh
}
function run(){
/usr/mcc/server/mcc-adms-web-service/bin/startup.sh
}

function install(){
  tar -zxvf $myFile -C /usr/mcc/server/
}

function unInstall(){
  rm -rf /usr/mcc/server/mcc-adms-web-service
  rm -rf /usr/mcc/logs/mcc-adms*
}

if [ "$OPTSTATUS" == '0' ];
then
  stop
  unInstall
elif [ "$OPTSTATUS" == '1' ];
  then
    install
elif [ "$OPTSTATUS" == '2' ];
  then
    stop
elif [ "$OPTSTATUS" == '3' ];
  then
    run
elif [ "$OPTSTATUS" == '4' ];
  then
    stop
    unInstall
    install
    run
else
  echo "You can only enter 0:uninstall/1:install/2:stop/3:run/4:unInstall install run"
fi
